﻿using Cheems.HMI.ViewModels.Interface;
using Cheems.Interface.Models.UserManagement;
using Cheems.Interface.UserManagement;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cheems.HMI.ViewModels.Base
{
    public class UserManagementVM : BindableBase, IUserManagementVM
    {
        internal IRegionManager _regionManager;
        internal IContainerProvider _containerProvider;
        internal IUIEventVM _uiEventVM;

        public UserManagementVM(IRegionManager regionManager, IContainerProvider containerProvider,IUIEventVM uIEventVM)
        {
            this._regionManager = regionManager;
            this._containerProvider = containerProvider;
            this._uiEventVM = uIEventVM;

            this._containerProvider.Resolve<IUserManagement>().OnUserLogin += UserManagementVM_OnUserLogin;
            this._containerProvider.Resolve<IUserManagement>().OnUserInfoChanged += UserManagementVM_OnUserInfoChanged;
            Task.Run(async () =>
            {
                CurrentUserList = await this._containerProvider.Resolve<IUserManagement>().QueryAllUser();
            });

            CurrentUser = new T_User()
            {
                Name = "Cheems",
                UserLevel = UserLevel.Engineer,
                Remarks= "DeepSeek-V3 在推理速度上相较历史模型有了大幅提升。\r\n\r\n在目前大模型主流榜单中，DeepSeek-V3 在开源模型中位列榜首，与世界上最先进的闭源模型不分伯仲。",
            };
        }

        /// <summary>
        /// 有用户登录时触发
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void UserManagementVM_OnUserLogin(T_User obj)
        {
            CurrentUser = obj;
        }

        /// <summary>
        /// 用户表发生改变时触发
        /// </summary>
        /// <param name="obj"></param>
        private void UserManagementVM_OnUserInfoChanged(List<T_User> obj)
        {
            CurrentUserList = obj;
        }

        private T_User _currentUser;
        public T_User CurrentUser
        {
            get { return _currentUser; }
            set { SetProperty(ref _currentUser, value); }
        }

        public List<T_User> CurrentUserList {  get; set; }

        private ObservableCollection<T_User> _currentDisplayUserlist;
        public ObservableCollection<T_User> CurrentDisplayUserlist
        {
            get { return _currentDisplayUserlist; }
            set { SetProperty(ref _currentDisplayUserlist, value); }
        }

        private DelegateCommand<string> _userLoggedIn;
        public DelegateCommand<string> UserLoggedInCommand =>
            _userLoggedIn ?? (_userLoggedIn = new DelegateCommand<string>(ExecuteUserLoggedInCommand));
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="parameter"></param>
        void ExecuteUserLoggedInCommand(string parameter)
        {
            this._uiEventVM.OpenWindow("LoginWindow");
        }

        private DelegateCommand<string> _userSwitched;
        public DelegateCommand<string> UserSwitchedCommand =>
            _userSwitched ?? (_userSwitched = new DelegateCommand<string>(ExecuteUserSwitchedCommand));
        /// <summary>
        /// 切换用户
        /// </summary>
        /// <param name="parameter"></param>
        void ExecuteUserSwitchedCommand(string parameter)
        {

        }

        private DelegateCommand<string> _userLoggedOut;
        public DelegateCommand<string> UserLoggedOutCommand =>
            _userLoggedOut ?? (_userLoggedOut = new DelegateCommand<string>(ExecuteUserLoggedOutCommand));
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <param name="parameter"></param>
        void ExecuteUserLoggedOutCommand(string parameter)
        {
            CurrentUser = null;
        }
    }
}
